package com.dxb.test.thread;

/**
 * Description：两个线程交替执行 <br>
 * author：戴晓波 <br>
 * date：2022/2/14/014 10:06 <br>
 */
public class AB {
    private Object lock = new Object();
    private volatile int count;

    private void print() {
        synchronized (lock) {
            while (count < 10) {
                System.out.println(Thread.currentThread().getName() + count++);
                lock.notifyAll();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            lock.notifyAll();
        }
    }

    public static void main(String[] args) {
        AB a = new AB();
        new Thread(() -> {
            a.print();
        }, "a").start();
        new Thread(() -> {
            a.print();
        }, "b").start();
    }
}
